package com.ruoyi.security.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.security.domain.AfElevator;
import com.ruoyi.security.domain.AfElevatorInfo;
import com.ruoyi.security.domain.ElevatorStatus;
import com.ruoyi.security.service.IAfElevatorInfoService;
import com.ruoyi.security.service.IAfElevatorService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "电梯事件回调接口")
@RestController
//@Controller
@RequestMapping("/interface")
@Slf4j
public class ElevatorInterfaceController {

    @Autowired
    private IAfElevatorService elevatorService;

    @Autowired
    private IAfElevatorInfoService elevatorInfoService;

    @PostMapping("/handleMessage")
    public AjaxResult handleMessage(String msg){
        log.info("电梯事件回调接口--->{}", msg);
        ElevatorStatus status = JSON.parseObject(msg, ElevatorStatus.class);
        //查询数据库，更新电梯状态;
        AfElevatorInfo afElevatorInfo = new AfElevatorInfo();
        afElevatorInfo.setRegCode(status.getData().getRegCode());
        List<AfElevatorInfo> infoList = elevatorInfoService.selectAfElevatorInfoList(afElevatorInfo);
        for(AfElevatorInfo info:infoList){
            AfElevator elevator = new AfElevator();
            elevator.setElevId(info.getElevId());
            List<AfElevator> afElevatorList = elevatorService.selectAfElevatorList(elevator);
            for(AfElevator afElevator:afElevatorList){
                //1 在线；2 离线
                if(status.getData().getStatus() ==1){
                    afElevator.setIsOnline("在线");
                }else{
                    afElevator.setIsOnline("离线");
                }
                elevatorService.updateAfElevator(afElevator);
            }
        }
        // 离线，在此处理
        log.info("interface: " + msg);
        return AjaxResult.success();
    }

}
